package org.lq.shopping.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.log4j.Logger;
import org.lq.shopping.dao.ShopVIPLevelDao;
import org.lq.shopping.entity.ShopShippingAddress;
import org.lq.shopping.entity.ShopVIPLevel;
import org.lq.shopping.util.DruidUtil;

public class ShopVIPLevelDaoImpl implements ShopVIPLevelDao {

	Logger log = Logger.getLogger(ShopVIPLevelDaoImpl.class);
	QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());

	@Override
	public int add(ShopVIPLevel t) {
		String sql = "insert into shop_viplevel"
				+ "  (vip_levelname, vip_discount, vip_min_integral, vip_max_integral, vip_deposit)" + "values"
				+ "  (nextval.VIPLevel_seq, ?, ?, ?, ?, ?);";
		log.debug("SQL语句:[" + sql + "]");
		log.debug("传入参数:[" + t + "]");
		int num = 0;
		try {
			num = qr.execute(sql, t.getVip_levelname(), t.getVip_discount(), t.getVip_min_integral(),
					t.getVip_max_integral(), t.getVip_deposit());
		} catch (SQLException e) {
			log.error("VIP管理的添加功能异常");
		}
		log.info("这是一个VIP管理的添加功能!");
		return num;
	}

	@Override
	public int delete(int id) {
		String sql = "delete shop_viplevel where vip_id = ?";
		log.debug("SQL语句:[" + sql + "]");
		log.debug("传入参数:[" + id + "]");
		int num = 0;
		try {
			num=qr.execute(sql, id);
		} catch (SQLException e) {
			log.error("VIP管理的删除功能异常");
		}
		log.info("这是一个VIP管理的删除功能!");
		return num;
	}

	@Override
	public int update(ShopVIPLevel t) {
		String sql = "update shop_viplevel" + "   set vip_levelname = ?," + "       vip_discount = ?,"
				+ "       vip_min_integral = ?," + "       vip_max_integral = ?," + "       vip_deposit = ?"
				+ " where vip_id = ?";
		log.debug("SQL语句:[" + sql + "]");
		log.debug("传入参数:[" + t + "]");
		int num = 0;
		try {
			num = qr.execute(sql, t.getVip_levelname(), t.getVip_discount(), t.getVip_min_integral(),
					t.getVip_max_integral(), t.getVip_deposit(), t.getVip_id());
		} catch (SQLException e) {
			log.error("VIP管理的修改功能异常");
		}
		log.info("这是一个VIP管理的修改功能!");
		return num;
	}

	@Override
	public List<ShopVIPLevel> findAll() {
		String sql = "select * from shop_VIPLevlel";
		log.debug("SQL语句:[" + sql + "]");
		List<ShopVIPLevel> list = new ArrayList<>();
		try {
			list = qr.query(sql, new BeanListHandler<>(ShopVIPLevel.class));
			
		} catch (SQLException e) {
			log.error("VIP管理的查询全部功能异常");
		}
		log.info("这是一个VIP管理的查询全部功能!");
		return list;
	}

	@Override
	public ShopVIPLevel findById(int id) {
		String sql = "select * from shop_VIPLevlel where vip_id =?";
		log.debug("SQL语句:[" + sql + "]");
		log.debug("传入参数:[" + id + "]");
		ShopVIPLevel shopVIPLevel = null;
		try {
			shopVIPLevel = qr.query(sql, new BeanHandler<>(ShopVIPLevel.class),id);
		} catch (SQLException e) {
			log.error("VIP管理的通过ID查询全部功能异常");
		}
		log.info("这是一个VIP管理的通过ID查询全部功能!");
		return shopVIPLevel;
	}

	@Override
	public List<ShopVIPLevel> LikeColumnByValue(String columName, Object value) {

	
		
		return null;
	}

	@Override
	public int getCount() {
		int num=0;
		String sql ="select count(1) from shop_VIPLevlel";
		log.debug("SQL语句:[" + sql + "]");
		try {
			num = qr.query(sql, new ScalarHandler<BigDecimal>()).intValue();
		} catch (SQLException e) {
			log.error(e);
		}
		return num;
	}

	@Override
	public List<ShopVIPLevel> findPage(int pageIndex, int pageSize) {
		List<ShopVIPLevel> list = null;
		String sql = "select * from "
				+ "(select rownum r,t.* from "
				+ "(select * from shop_VIPLevlel order by vip_id) t"
				+ " where rownum<=?)"
				+ "where r > ?";
		int max = pageIndex*pageSize;
		int min = (pageIndex-1) * pageSize;
		log.debug("SQL语句:[" + sql + "]");
		log.debug("传入参数:[" + pageIndex+"-----"+pageSize + "]");
		try {
			list = qr.query(sql, new BeanListHandler<ShopVIPLevel>(ShopVIPLevel.class),max,min);
		} catch (SQLException e) {
			log.error(e);
		}
		return list;
	}

}
